import { message } from "antd";
import { copy as statCopy } from "./stat";
import { ICategory } from "./types";

export function onCopy(type: string, id: number) {
	message.success('复制成功');
	statCopy(type, id);
}

export function getVideoId(url: string) {
	if (url.match(/^(qq|bilibili|youku)\|(.*?)/)) {
		return url;
	} else if (url.indexOf('bilibili.com') > 0 || url.indexOf('b23.tv') > 0) {
		const av = url.match(/av(\d+)/);
		return av ? "bilibili|" + av[1] : url;
	} else if (url.indexOf('youku.com') > 0) {
		const id = url.match(/id_(.*?)\.html/);
		return id ? "youku|" + id[1] : url;
	} else if (url.indexOf('v.qq.com') > 0) {
		if (url.indexOf('x/page') > 0) {
			const id = url.match(/page\/(.*?)\.html/);
			return id ? "qq|" + id[1] : url;
		} else {
			const id = url.match(/cover\/(.*?)\/(.*?).html/);
			return id ? "qq|" + id[2] : url;
		}
	}
	return null;
}

export function getCategory(category: ICategory[], id: number) {
	for (const it of category) {
		if (it.id === id) {
			return it.name;
		}
	}
	return "";
}